package com.longnuoqi.mydoctor.fragment;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ExpandableListView;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.longnuoqi.mydoctor.entity.Disease;
import com.longnuoqi.mydoctor.utils.ExpandableListUtil;
import com.longnuoqi.mydoctor.utils.FileUtil;
import com.longnuoqi.mydoctor.utils.OtherUtil;
import com.yunhe.mydoctor.R;


import java.util.ArrayList;

import java.util.List;


/**
 * Created by Lenovo on 2015/12/26.
 */
public class ZizhenListFragment extends Fragment {
    private int id;
    private int position;
    private String name;
    private String url;
    private ExpandableListView listView;
    private List<Disease> departmentList;
    private List<Disease> list;
    private List<List<Disease>> lists;
    private String[] category;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.zizhen_classify_list_layout, container, false);
        savedInstanceState = getArguments();
        id = savedInstanceState.getInt("id");
        position = savedInstanceState.getInt("position");
        init(view);
        return view;
    }


    private void init(View view) {
        listView = (ExpandableListView) view.findViewById(R.id.zizhen_expandablelist);
        name = "disease_department_list.txt";
        url = "http://www.tngou.net/api/department/all";
        if (!FileUtil.fileIsExist(name)) {
            Log.e("==============", "file:" + name + ":is not exist");
            RequestQueue queue = Volley.newRequestQueue(getActivity());
            StringRequest request = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
                @Override
                public void onResponse(String s) {
                    FileUtil.saveFile(s, name);
                    departmentList = FileUtil.jsonDepartmentList(s, position);
                    getData();
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError volleyError) {
                }
            });
            queue.add(request);
        } else {
            Log.e("==============", "file:" + name + ":exist");
            String s = FileUtil.readFile(name);
            departmentList = FileUtil.jsonDepartmentList(s, position);
            getData();
        }
    }

    private void getData() {
        list = new ArrayList<>();
        lists = new ArrayList<>();
        category = new String[departmentList.size()];
        for (int i = 0; i < departmentList.size(); i++) {
            Log.e("==============", "category:" + i + ":" + departmentList.get(i).getName() + "categoryid:" + departmentList.get(i).getId());
            category[i] = departmentList.get(i).getName();
            url = "http://www.tngou.net/api/disease/department?id=" + departmentList.get(i).getId() + "&rows=6";
            name = "disease_department_list_" + departmentList.get(i).getId() + ".txt";
            if (!FileUtil.fileIsExist(name)) {
                Log.e("==============", "file:" + name + ":is not exist");
                RequestQueue queue = Volley.newRequestQueue(getActivity());
                StringRequest request = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
                    @Override
                    public void onResponse(String s) {
                        FileUtil.saveFile(s, name);
                        Log.e("===============", "list_s:" + s);
                        list = FileUtil.jsonDiseaseList(s);
                        lists.add(list);
                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError volleyError) {
                    }
                });
                queue.add(request);
            } else {
                Log.e("==============", "file:" + name + ":exist");
                String s = FileUtil.readFile(name);
                Log.e("===============", "list_s:" + s);

                list = FileUtil.jsonDiseaseList(s);
                lists.add(list);
            }
        }
        ExpandableListUtil.expandableList(getActivity(), listView, lists, category);
        OtherUtil.showProgress(getActivity(), "", "数据加载较慢，请稍后……", 9000);
    }

}
